<template>
  <div>
    <!-- 一条文章单元格 -->
    <van-cell>
      <!-- 标题区域插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ articleObj.title }}</span>
          <!-- 单图 -->
          <!-- <img
            v-if="articleObj.cover.type === 1"
            class="thumb"
            :src="articleObj.cover.images[0]"
            alt=""
          /> -->
          <!-- 图片加载失败提示 -->
          <van-image
            v-if="articleObj.cover.type === 1"
            class="thumb"
            :src="articleObj.cover.images[0]"
            :lazy-load="true"
          >
            <template v-slot:error>加载失败</template>
          </van-image>
        </div>
        <!-- 三图，图片容器 -->
        <div class="thumb-box" v-if="articleObj.cover.type > 1">
          <!-- <img
            v-for="(imgUrl, index) in articleObj.cover.images"
            :key="index"
            :src="imgUrl"
            alt=""
            class="thumb"
          /> -->
          <!-- 图片加载失败提示 -->
          <van-image
            v-for="(imgUrl, index) in articleObj.cover.images"
            :key="index"
            :src="imgUrl"
            alt=""
            class="thumb"
            :lazy-load="true"
          >
            <template v-slot:error>加载失败</template>
          </van-image>
        </div>
      </template>
      <!-- label区域插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ articleObj.aut_name }}</span>
            <span>{{ articleObj.comm_count }}评论</span>
            <span>{{ formatTime(articleObj.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click.stop="show = true" v-if="isShow" />
        </div>
      </template>
    </van-cell>

    <!-- 反馈面板
    get-container设置该反馈面板的挂载点
    cancel-text取消按钮文字
    -->
    <van-action-sheet
      v-model="show"
      :actions="actions"
      @select="onSelect"
      @cancel="onCancel"
      @close="onClose"
      get-container="body"
      :cancel-text="bottomText"
    />
  </div>
</template>

<script>
import { timeAgo } from "@/utils/date.js";
import { firstActions, secondActions } from "@/utils/report.js";

export default {
  props: {
    articleObj: Object, //存放文章对象
    //反馈按钮是否显示，默认显示
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      show: false, //反馈面板是否打开
      actions: firstActions, //存放反馈面板选项
      bottomText: "取消",
    };
  },
  methods: {
    //格式化时间
    formatTime: timeAgo,

    //选择反馈面板中的选项时触发
    onSelect(action, index) {
      //action为当前点击的选项{name:"反馈垃圾内容"}
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      // this.show = false;
      if (action.name === "反馈垃圾内容") {
        this.actions = secondActions;
        this.bottomText = "返回";
      } else if (action.name === "不感兴趣") {
        //给父组件传递信息
        this.$emit("dislikeEV", this.articleObj.art_id);
        //关闭反馈面板
        this.show = false;
      }
      //二级反馈内容
      else {
        this.$emit("reportEV", this.articleObj.art_id, action.type);
        this.show = false; //关闭时cancelFn会进行判断
      }
    },

    //点击返回按钮触发
    onCancel() {
      if (this.bottomText === "返回") {
        this.show = true;
        this.actions = firstActions;
        this.bottomText = "取消";
      }
    },

    //关闭面板时触发
    onClose() {
      this.actions = firstActions;
      this.bottomText = "取消";
    },
  },
};
</script>

<style scoped lang="less">
//标题样式
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

//label描述样式
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

// 文章信息
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0px;
  }
}

//图片的样式
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

//三图，图片容器
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
